package com.icss.train_sys_back.controller.admin.system_manage;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.icss.train_sys_back.entity.ResponseResult;
import com.icss.train_sys_back.entity.ResultEnum;
import com.icss.train_sys_back.entity.system_manage.RoleEntity;
import com.icss.train_sys_back.service.system_service.RoleManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 小泽
 * @createTime 2025/9/29 9:52
 */

//系统管理controller
@RestController
@RequestMapping("/admin/role_manage")
@Slf4j
public class RoleManageController {
    @Autowired
    private RoleManageService roleManageService;

    //分页+条件查询角色列表
    @GetMapping("/getList")
    public ResponseResult getListConditions(@RequestParam(defaultValue = "1") int page,
                                            @RequestParam(defaultValue = "10") int pageSize,
                                            @RequestParam(required = false) String roleName,
                                            @RequestParam(required = false) String roleDescription) {

        log.info("分页+条件查询角色列表  page={} , pageSize={},roleName={},description={}", page, pageSize, roleName, roleDescription);
        PageHelper.startPage(page, pageSize);
        List<RoleEntity> roleEntityList = roleManageService.getListConditions(roleName, roleDescription);
        PageInfo<RoleEntity> pageInfo = new PageInfo<>(roleEntityList);
        return ResponseResult.success(pageInfo);
    }

    //添加角色
    @PostMapping("/add")
    public ResponseResult add(@RequestBody RoleEntity role) {
        log.info("添加角色 role = {}", role);
        boolean res = roleManageService.add(role);
        return res ? ResponseResult.success() : ResponseResult.fail(ResultEnum.FAIL);
    }

    //删除角色
    @DeleteMapping("/delete/{roleId}")
    @Transactional
    public ResponseResult delete(@PathVariable("roleId") Integer roleId) {
        log.info("根据roleId删除角色 roleId = {}", roleId);
        //根据角色id删除对应的权限
        roleManageService.deletePermissionsByRoleId(roleId);
        roleManageService.delete(roleId);
        return ResponseResult.success();
    }

    //更新角色
    @PostMapping("/update")
    public ResponseResult update(@RequestBody RoleEntity role) {
        log.info("更新角色 role = {}", role);
        boolean res = roleManageService.update(role);
        return res ? ResponseResult.success() : ResponseResult.fail(ResultEnum.FAIL);
    }

    //根据角色Id获取角色对应的权限Ids
    @GetMapping("/getRolePermissions")
    public ResponseResult getRolePermissions(@RequestParam Integer roleId) {
        log.info("根据角色Id获取角色对应的权限Ids roleId = {}", roleId);
        List<Integer> permissions = roleManageService.getRolePermissions(roleId);
        return ResponseResult.success(permissions);
    }

    //根据角色Id保存对应的权限
    @PostMapping("/saveRolePermissions/{roleId}")
    @Transactional
    public ResponseResult saveRolePermissions(@PathVariable("roleId") Integer roleId,
                                              @RequestBody List<Integer> permissionIds) {
        log.info("根据角色Id保存对应的权限 roleId = {} , 选中的权限Id列表 = {}", roleId, permissionIds);
        //先根据角色Id删除原有的权限
        roleManageService.deletePermissionsByRoleId(roleId);
        //再插入
        roleManageService.insertRolePermissions(roleId, permissionIds);
        return ResponseResult.success();


    }

    //获取系统中所有角色的列表，为用户管理页面中的角色选择下拉框提供数据
    @GetMapping("/getAllRoles")
    public ResponseResult getAllRoles() {
        log.info("获取系统中所有角色的列表方法");
        List<RoleEntity> roles = roleManageService.getAllRoles();
        log.info("结果：{}", roles);
        return ResponseResult.success(roles);
    }

}
